package com.kevin.zhangchao.androidtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * Created by ZhangChao on 2016/12/11.
 */

public class PwdEditText extends EditText {

    private Paint mPwdPaint;
    private Paint mRectPaint;
    private int mWidth;
    private int mHeight;
    private int PWD_LENGTH=6;
    private int PWD_SPACING=5;
    private float PWD_SIZE=5;
    private int mInputLenght;
    private Rect mRect;
    private OnInputFinishListener mOnInputFinishListener;

    public PwdEditText(Context context) {
        super(context);
        init(context,null);
    }

    public PwdEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);

    }

    public PwdEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);

    }

    private void init(Context context, AttributeSet attrs) {
        mPwdPaint=new Paint();
        mPwdPaint.setColor(Color.BLACK);
        mPwdPaint.setStyle(Paint.Style.FILL);
        mPwdPaint.setAntiAlias(true);

        mRectPaint=new Paint();
        mRectPaint.setStyle(Paint.Style.STROKE);
        mRectPaint.setColor(Color.LTGRAY);
        mRectPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth=getWidth();
        mHeight=getHeight();

        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(0,0,mWidth,mHeight,paint);

        int rectWidth=(mWidth-PWD_SPACING*(PWD_LENGTH-1))/PWD_LENGTH;

        for (int i=0;i<PWD_LENGTH;i++){
            int left=(rectWidth+PWD_SPACING)*i;
            int top=2;
            int right=left+rectWidth;
            int bottom=mHeight-top;
            mRect=new Rect(left,top,right,bottom);
            canvas.drawRect(mRect,mRectPaint);
        }

        for (int i=0;i<mInputLenght;i++){
            int cx=rectWidth/2+(rectWidth+PWD_SPACING)*i;
            int cy=mHeight/2;
            canvas.drawCircle(cx,cy,PWD_SIZE,mPwdPaint);
        }

    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        this.mInputLenght=text.toString().length();
        invalidate();
        if (mInputLenght==PWD_LENGTH&&mOnInputFinishListener!=null){
            mOnInputFinishListener.onInputFinish(text.toString());
        }
    }

    public interface OnInputFinishListener{
        void onInputFinish(String password);
    }

    public void setOnInputFinishListener(
            OnInputFinishListener onInputFinishListener) {
        this.mOnInputFinishListener = onInputFinishListener;
    }
}
